<template>
  <div>
    <el-dialog
      :title="obj.isAdd == true ? '规格添加' : '规格编辑'"
      :visible.sync="obj.isTrue"
      @close="cancel()"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="规格名称">
          <el-input v-model="form.specsname"></el-input>
        </el-form-item>
      </el-form>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item
          label="规格属性"
          v-for="(item, index) in attrarr"
          :key="index"
        >
          <div class="flex">
            <el-input v-model="item.val"></el-input>
            <el-button type="info" v-if="index == 0" @click="attradd()"
              >新增规格属性</el-button
            >
            <el-button type="success" v-else @click="dele(index)">
              删除</el-button
            >
          </div>
        </el-form-item>
      </el-form>

      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button @click="submit()" v-if="obj.isAdd == true">添 加</el-button>
        <el-button type="primary" v-else @click="dits()">编辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { successAlert, warningAlert } from "../../../util/alter.js";
import { specsAdd, specsInfo,specsEdit } from "../../../requster/api";
import {
  manageList,
  manageCount,
  manageDel,
  manageInfo,
  manageEdit,
} from "../../../requster/api.js";
import { mapGetters, mapActions } from "vuex";
export default {
  //接收父组件传来的参数
  props: ["obj"],
  data() {
    return {
      // 渲染多少栏规格属性
      attrarr: [
        {
          val: "",
        },
        {
          val: "",
        },
      ],

      form: {
        specsname: "",
        attrs: "",
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      roleList: "role/list",
    }),
  },

  methods: {
    //   增加规格属性
    attradd() {
      this.attrarr.push({ val: "" });
    },
    // 删除规格
    dele(index) {
      this.attrarr.splice(index, 1);
    },

    ...mapActions({
      reqroleList: "role/reqchangeList",
      reqspecslist: "specs/reqchangelist",
    }),
    empty() {
      //清空封装
      this.form = {
        specsname: "",
        attrs: "",
        status: 1,
      };
      this.attrarr = [{ val: "" }, { val: "" }];
    },
    cancel() {
      //取消事件
      //改变父组件的传来的isTure
      this.obj.isTrue = false;

      this.empty();
    },
    submit() {
      this.form.attrs = JSON.stringify(
        this.attrarr.map((item) => {
          return item.val;
        })
      );
      //点击添加按钮
      specsAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.reqspecslist();
          this.cancel();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    // 获取一条数据
    getone(id) {
      specsInfo({ id: id }).then((res) => {
        let arr = JSON.parse(res.data.list[0].attrs);
       let arr1= arr.map((item) => {
          return { val: item };
        });
        this.attrarr=arr1
        this.form.id = id;
        this.form = res.data.list[0];
      });
    },
    dits() {
    this.form.attrs=JSON.stringify(this.attrarr.map(item=>item.val))
      specsEdit(this.form).then((res) => {
        console.log(this.form);
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.reqspecslist();
          this.cancel();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
  },
  mounted() {
    this.reqroleList();
  },
};
</script>

<style scoped>
.flex {
  display: flex;
}
</style>